package com.jmp.api.service.impl;

import com.jmp.api.dto.MessagePushDto;
import com.jmp.api.service.MessageService;
import com.jmp.api.service.TemplateCardService;
import com.jmp.base.MessageApi;
import com.jmp.base.TemplateApi;
import com.jmp.base.dto.MessagePushType;
import one.stand.advice.ResultEnum;
import one.stand.mapper.TemplateConfigMapperExt;
import one.stand.mapper.TemplateMapperExt;
import one.stand.model.Template;
import one.stand.model.TemplateConfig;
import one.stand.service.sys.UserContentService;
import one.stand.util.AssertUtil;
import one.stand.util.OneListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class TemplateCardServiceImpl implements TemplateCardService {
    @Autowired
    private TemplateMapperExt templateMapperExt;
    @Autowired
    private TemplateConfigMapperExt templateConfigMapperExt;
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserContentService userContentService;

    @Override
    @Transactional
    public Integer save(Template template, List<TemplateConfig> templateConfigs) {
        Integer id = save(template.getId(), template.getName(), template.getImgUrl(), template.getCompanyId(),
                template.getImgWidth(), template.getImgHeight(), template.getImgUrlWidth(), template.getImgUrlHeigth(),
                template.getPositionWidth(), template.getPositionHeight());

        if (Objects.nonNull(template.getId())) {
            templateConfigMapperExt.deleteByTempalteId(template.getId());
        }
        int i = 1;
        for (TemplateConfig config : templateConfigs) {
            config.setTemplateId(id);
            config.setSort(i++);
            templateConfigMapperExt.insertSelective(config);
        }

        return id;
    }

    @Override
    @Transactional
    public void up(Integer id, String pushTitle, String pushContent, String labels) {
        Template template = templateMapperExt.selectByPrimaryKey(id);
        AssertUtil.nonNull(template, ResultEnum.PARAM_CHECK);
        AssertUtil.nonNull(template.getName(), "名称不能为空");
        AssertUtil.nonNull(template.getImgUrl(), "背景图不能为空");
//        AssertUtil.nonNull(template.getImgHtml(), "模板不能为空");
//        AssertUtil.nonNull(template.getImgWidth(), "背景图-宽不能为空");
//        AssertUtil.nonNull(template.getImgHeight(), "背景图-高不能为空");

        Template templateUpdate = new Template();
        templateUpdate.setId(id);
        templateUpdate.setState(TemplateApi.STATE_DEPLOY);
        templateUpdate.setStateTime(new Date());
        templateMapperExt.updateByPrimaryKeySelective(templateUpdate);

        userContentService.add(UserContentService.TYPE_CARD, id, labels, UserContentService.TYPE_COMPANY);

        // push
        if (StringUtils.isNoneBlank(labels)) {
            MessagePushDto pushDto = MessagePushDto.builder()
                    .type(MessagePushType.CARD_UP)
                    .toType(MessageApi.TYPE_LABELS_COMPANY)
                    .toIds(OneListUtil.toList(labels, Integer::valueOf))
                    .title(pushTitle)
                    .fillData(new String[]{pushContent})
                    .build();
            messageService.push(pushDto);
        } else if (!TemplateApi.DEFAULT_COMPANYID.equals(template.getCompanyId())) {
            MessagePushDto pushDto = MessagePushDto.builder()
                    .type(MessagePushType.CARD_UP)
                    .toType(MessageApi.TYPE_COMPANY)
                    .toId(template.getCompanyId())
                    .title(pushTitle)
                    .fillData(new String[]{pushContent})
                    .build();
            messageService.push(pushDto);
        } else {
            MessagePushDto pushDto = MessagePushDto.builder()
                    .type(MessagePushType.CARD_UP)
                    .title(pushTitle)
                    .fillData(new String[]{pushContent})
                    .build();
            messageService.pushAll(pushDto);
        }
    }

    @Override
    @Transactional
    public void down(Integer id) {
        Template templateUpdate = new Template();
        templateUpdate.setId(id);
        templateUpdate.setState(TemplateApi.STATE_DRAFT);
        templateMapperExt.updateByPrimaryKeySelective(templateUpdate);

        userContentService.remove(UserContentService.TYPE_CARD, id);
    }

    private int save(Integer id, String name, String imgUrl, Integer companyId, Integer imgWidth, Integer imgHeight,
                     Integer imgUrlWidth, Integer imgUrlHeigth, Integer positionWidth, Integer positionHeight) {
        if (Objects.nonNull(id)) {

            Template template = templateMapperExt.selectByPrimaryKey(id);
            AssertUtil.nonNull(template, ResultEnum.PARAM_CHECK);
            AssertUtil.isTrue(template.getState() != TemplateApi.STATE_DEPLOY, "海报已发布，不能再修改");

            template = new Template();
            template.setId(id);
            template.setName(name);
            template.setImgUrl(imgUrl);
//            template.setImgHtml(imgHtml);
            template.setImgWidth(imgWidth);
            template.setImgHeight(imgHeight);
            template.setImgUrlWidth(imgUrlWidth);
            template.setImgUrlHeigth(imgUrlHeigth);
            template.setPositionWidth(positionWidth);
            template.setPositionHeight(positionHeight);
            templateMapperExt.updateByPrimaryKeySelective(template);
            return id;
        } else {
            Template template = new Template();
            template.setName(name);
            template.setImgUrl(imgUrl);
            template.setCompanyId(companyId);
//            template.setImgHtml(imgHtml);
            template.setImgWidth(imgWidth);
            template.setImgHeight(imgHeight);
            template.setImgUrlWidth(imgUrlWidth);
            template.setImgUrlHeigth(imgUrlHeigth);
            template.setPositionWidth(positionWidth);
            template.setPositionHeight(positionHeight);
            template.setState(TemplateApi.STATE_DRAFT);
            templateMapperExt.insertSelective(template);
            return template.getId();
        }
    }
}
